package com.pay.ioopos.activity;

import android.content.Intent;
import android.os.Bundle;

import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.PwdListener;
import com.pay.ioopos.R;
import com.pay.ioopos.StatusListener;
import com.pay.ioopos.fragment.ComPwdSetFragment;
import com.pay.ioopos.fragment.DeviceInfoFragment;
import com.pay.ioopos.fragment.FunctionSwitchFragment;
import com.pay.ioopos.fragment.SuperPwdSetFragment;
import com.pay.ioopos.fragment.SuperPwdValidFragment;
import com.pay.ioopos.fragment.SystemFragment;
import com.pay.ioopos.fragment.TipVerticalFragment;

import static com.pay.ioopos.KeyInfo.KEY_CANCEL;
import static com.pay.ioopos.KeyInfo.KEY_ENTER;
import static com.pay.ioopos.KeyInfo.KEY_NUM_1;
import static com.pay.ioopos.KeyInfo.KEY_NUM_2;
import static com.pay.ioopos.KeyInfo.KEY_NUM_3;
import static com.pay.ioopos.KeyInfo.KEY_NUM_4;
import static com.pay.ioopos.KeyInfo.KEY_NUM_5;
import static com.pay.ioopos.fragment.TipFragment.TipType.SUCCESS;

public class SystemActivity extends AbstractActivity implements PwdListener, StatusListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system);
        setMainFragment(new SystemFragment());
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
            if (mainFragment instanceof SystemFragment) {
                Intent intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
            } else {
                setMainFragment(new SystemFragment());
            }
            return true;
        } else if (mainFragment instanceof SuperPwdValidFragment) {
            SuperPwdValidFragment fragment = ((SuperPwdValidFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            return true;
        } else if (mainFragment instanceof SuperPwdSetFragment) {
            SuperPwdSetFragment fragment = ((SuperPwdSetFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            return true;
        } else if (mainFragment instanceof ComPwdSetFragment) {
            ComPwdSetFragment fragment = ((ComPwdSetFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            return true;
        } else if (keyInfo == KEY_NUM_1) {
            Intent intent = new Intent(this, SoundActivity.class);
            startActivity(intent);
            return true;
        } else if (keyInfo == KEY_NUM_2) {
            SuperPwdValidFragment fragment = new SuperPwdValidFragment();
            fragment.setPwdListener(this);
            setMainFragment(fragment);
            return true;
        } else if (keyInfo == KEY_NUM_3) {
            ComPwdSetFragment fragment = new ComPwdSetFragment();
            fragment.setListener(this);
            setMainFragment(fragment);
        } else if (keyInfo == KEY_NUM_4) {
            SuperPwdSetFragment fragment = new SuperPwdSetFragment();
            fragment.setListener(this);
            setMainFragment(fragment);
            return true;
        } else if (keyInfo == KEY_NUM_4) {
            setMainFragment(new DeviceInfoFragment());
            return true;
        } else if (keyInfo == KEY_NUM_5) {
            Intent intent = new Intent(this, UpdateActivity.class);
            startActivity(intent);
            return true;
        }
        return false;
    }

    @Override
    public void onPwdValid(String pwd) {
        setMainFragment(new FunctionSwitchFragment());
    }

    @Override
    public void onSuccess(String msg) {
        setMainFragment(new TipVerticalFragment(SUCCESS, msg));
    }
}
